package cn.iocoder.yudao.module.crm.controller.admin.levelconfig;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.crm.controller.admin.label.vo.CrmLabelPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.label.vo.CrmLabelRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.label.vo.CrmLabelSaveReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.levelconfig.vo.CrmLevelConfigPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.levelconfig.vo.CrmLevelConfigRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.levelconfig.vo.CrmLevelConfigSaveReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.label.CrmLabelDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.levelconfig.CrmLevelConfigDO;
import cn.iocoder.yudao.module.crm.service.label.CrmLabelService;
import cn.iocoder.yudao.module.crm.service.levelconfig.CrmLevelConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 客户标签")
@RestController
@RequestMapping("/crm/level-config")
@Validated
public class CrmLevelConfigController {

    @Resource
    private CrmLevelConfigService crmLevelConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建客户级别配置")
    @PreAuthorize("@ss.hasPermission('crm:module:create')")
    public CommonResult<Long> createLevelConfig(@Valid @RequestBody CrmLevelConfigSaveReqVO createReqVO) {
        return success(crmLevelConfigService.createLevelConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户标签")
    @PreAuthorize("@ss.hasPermission('crm:module:update')")
    public CommonResult<Boolean> updateLevelConfig(@Valid @RequestBody CrmLevelConfigSaveReqVO updateReqVO) {
        crmLevelConfigService.updateLevelConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户标签")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:module:delete')")
    public CommonResult<Boolean> deleteLevelConfig(@RequestParam("id") Long id) {
        crmLevelConfigService.deleteLevelConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户标签")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CrmLevelConfigRespVO> getLevelConfig(@RequestParam("id") Long id) {
        CrmLevelConfigDO data = crmLevelConfigService.getLevelConfig(id);
        return success(BeanUtils.toBean(data, CrmLevelConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户标签分页")
    public CommonResult<PageResult<CrmLevelConfigRespVO>> getLabelPage(@Valid CrmLevelConfigPageReqVO pageReqVO) {
        PageResult<CrmLevelConfigDO> pageResult = crmLevelConfigService.getLevelConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CrmLevelConfigRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得客户级别列表")
    public CommonResult<List<CrmLevelConfigRespVO>> getLeveConfigList(@Valid @RequestParam(value = "moduleId", required = false) Long moduleId) {
        CrmLevelConfigPageReqVO pageReqVO = new CrmLevelConfigPageReqVO();
        pageReqVO.setModuleId(moduleId);
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CrmLevelConfigDO> pageResult = crmLevelConfigService.getLevelConfigPage(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, CrmLevelConfigRespVO.class));
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出客户标签 Excel")
//    @PreAuthorize("@ss.hasPermission('crm:label:export')")
//    @OperateLog(type = EXPORT)
//    public void exportLabelExcel(@Valid CrmLabelPageReqVO pageReqVO,
//                                 HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<CrmLabelDO> list = labelService.getLabelPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "客户标签.xls", "数据", CrmLabelRespVO.class,
//                BeanUtils.toBean(list, CrmLabelRespVO.class));
//    }


    @PostMapping("/create-default")
    @Operation(summary = "创建客户级别配置-系统默认")
    @PreAuthorize("@ss.hasPermission('crm:module:create-default')")
    public CommonResult<Long> createLevelConfigDefault(@Valid @RequestBody CrmLevelConfigSaveReqVO createReqVO) {

        return success(TenantUtils.execute(0L, () -> crmLevelConfigService.createLevelConfig(createReqVO)));
    }

    @PutMapping("/update-default")
    @Operation(summary = "更新客户标签-系统默认")
    @PreAuthorize("@ss.hasPermission('crm:module:update-default')")
    public CommonResult<Boolean> updateLevelConfigDefault(@Valid @RequestBody CrmLevelConfigSaveReqVO updateReqVO) {
        TenantUtils.execute(0L, () -> crmLevelConfigService.updateLevelConfig(updateReqVO));
        return success(true);
    }

    @DeleteMapping("/delete-default")
    @Operation(summary = "删除客户标签-系统默认")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:module:delete-default')")
    public CommonResult<Boolean> deleteLevelConfigDefault(@RequestParam("id") Long id) {
        TenantUtils.execute(0L, () -> crmLevelConfigService.deleteLevelConfig(id));
        return success(true);
    }


    @GetMapping("/get-default")
    @Operation(summary = "获得客户标签-系统默认")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CrmLevelConfigRespVO> getLevelConfigDefault(@RequestParam("id") Long id) {
        CrmLevelConfigDO data = TenantUtils.execute(0L, () -> crmLevelConfigService.getLevelConfig(id));
        return success(BeanUtils.toBean(data, CrmLevelConfigRespVO.class));
    }


    @GetMapping("/page-default")
    @Operation(summary = "获得客户标签分页-系统默认")
    public CommonResult<PageResult<CrmLevelConfigRespVO>> getLabelPageDefault(@Valid CrmLevelConfigPageReqVO pageReqVO) {
        PageResult<CrmLevelConfigDO> pageResult = TenantUtils.execute(0L, () -> crmLevelConfigService.getLevelConfigPage(pageReqVO));
        return success(BeanUtils.toBean(pageResult, CrmLevelConfigRespVO.class));
    }
}